var googlemap = function(param){

	//param
		// id
		// zoom
		// pref
		// address
	
	var that = {};
	
	that.setMapModal = function(){
		if($('#mapModal').size() == 0){
			var html = '<div id="mapModal" class="mcon">';
			html += '<span class="close_btn"><a href="javascript:;">b</a></span>';
			html += '<h3>地図クリック、アイコンドラッグで座標を設定出来ます</h3>';
			html += '<div id="' + param.id + '" style="width : 100%; height : 500px;"></div>';
			html += '<div class="modal_btns"><p><input type="button" value="保存する" id="save_position" /></p></div>';
			html += '</div>';
			$('body').append(html);
			$("#mapModal").draggable({handle:"h3"});
			
			$('#save_position').click(function(){
				that.savePosition();
			});
		}
		that.toggleMapModal(true);
	};
	
	that.savePosition = function(){
		$('#pos_status').text("設定済");
		$('#status_on').show();
		$('#status_off').hide();
		$('#lat').val(that.marker.getPosition().lat());
		$('#lng').val(that.marker.getPosition().lng());
		that.toggleMapModal(false);
	};
	
	that.toggleMapModal = function(s){
		if(s == true){
			$('body').append('<div id="modal_background">&nbsp;</div>');
			$('#modal_background').fadeIn(300).height($(document).height());
			$('#mapModal')
				.show()
				.css({
					"position":"absolute",
					"opacity":0,
					'top':($(document).scrollTop())+'px'})
				.animate({"opacity":1,"top":($(document).scrollTop()+50)+"px"},300,function(){
					that.renderMap();
				});
			$('#modal_background , .close_btn').click(function(){
				that.toggleMapModal(false);
			});
		}else {
			$('#modal_background').fadeOut(300,function(){$(this).remove();});
			$('#mapModal')
				.css({"position":"absolute"})
				.animate({"opacity":0,"top":$(document).scrollTop()+'px'},300,
				function(){
					$(this).hide();
				});
		}
	}
	
	that.renderMap = function(){
		if(!that.map){
			var mapdiv = document.getElementById(param.id);
			var myOptions = {
				zoom: param.zoom,
				center: new google.maps.LatLng(0,0),
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				scaleControl: true,
			};
			that.map = new google.maps.Map(mapdiv, myOptions);
		}
		that.setPosition();
	};
	
	that.setPosition = function(){
		
		var latlng;
		var x = ($('#lat').val()) ? $('#lat').val() : 0;
		var y = ($('#lng').val()) ? $('#lng').val() : 0;
		var address = $("option:selected",param.pref).text() + $(param.address).val();
		
		if(!x && $("option:selected",param.pref).val() != 0){
			//ジオコーディング
			var geocoder = new google.maps.Geocoder();
			
			geocoder.geocode( { 'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					latlng = results[0].geometry.location;
					that.map.setCenter(latlng);
					that.addMarker(latlng);
				} else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
		}else {
			latlng = new google.maps.LatLng( x , y );
			that.map.setCenter(latlng);
			that.addMarker(latlng);
		}
		
		google.maps.event.addListener(that.map, 'click', function(e){ 
			that.marker.position = e.latLng;
			that.marker.setMap(that.map); 
		});
	};
	
	that.addMarker = function(latlng){
		if(that.marker){
			that.marker.setMap(null);
		}
		
		that.marker = new google.maps.Marker({
			map: that.map, 
			position: latlng,
			draggable: true,
			icon: "/web-admin/assets/images/icons/home.png"
		});
	};
	
	return that;
	
}